~function() {
	/*
	opt.selector  选择器对象->如$('#id'), $('.class')
	opt.code 前后端引擎中接口标识
	opt.setting  excel表格设置
	opt.change  是否行转列
	opt.simulate  是否用模拟数据
	*/
	var Excel = function(opt) {
		var url = global.cxt + '/webexcel/' + opt.code;
		var setting = $.extend(true, {}, Excel.setting.blank);
		var	hot = new Handsontable(opt.selector[0], setting);
		var change = function(data) {
			var result = [];
			//数组
			if(data[0].length > -1) {
				$(res.data).each(function(i, e) {
					$(e).each(function(ii, ee) {
						if(!result[ii]) result[ii] = [];
						result[ii][i] = ee;
					});
				});
			} else {//json {}
				$(data).each(function(i, e) {
					var ii = 0;
					for(var ee in e) {
						if(!result[ii]) result[ii] = {};
						result[ii][ee] = e[ee];
						ii ++;
					}
				});
			}
			return result;
		}
		
		//模拟数据
		if(opt.simulate) {
			data = Handsontable.helper.createSpreadsheetData(200, 200);
		} else {
		//上线数据
			$.ajax({
				url: url,
				async:false
			}).done(function(res){
				if(!res || res.code!=0) throw new Error('接口响应不正确');
				data = res.data;
			});
		}
		
		//行转列
		if(opt.change) { data = change(data); }
		
		setting.data = data;
		
		/**********公共方法************/
		//装载表格
		this.load = function() {
			//获取设置
			$.ajax({
				url: global.cxt + '/webexcel/getSetting',
				data: {code: opt.code, fullget: true},
				async:false
			}).done(function(res){
				if(!res || res.code!=0) throw new Error('接口响应不正确');
				$.extend(true, setting, res.data);
			});
			$(setting.cell).each(function(i, e) {
				e.renderer = Excel.renderer;
			});
			hot.updateSettings(setting);
			return this;
		}
		
		//保存设置
		this.save = function() {
			//setting.data = hot.getData();
			var _setting = {};
			$.extend(true, _setting, setting);
			delete _setting.data;
			
			$.ajax({
				url: global.cxt + '/webexcel/saveSetting',
				data: {
					code: opt.code,
					setting: JSON.stringify(_setting)
				},
				async:false
			}).done(function(res){
				console.log('setting success');
			});
			return this;
		}
		
		//合并单元格
		this.mergeCells = function(){
	 		 var selectObj = hot.getSelected();
			 var row = selectObj[0];
			 var col = selectObj[1];
			 var rowspan = selectObj[2] - selectObj[0]+1;
			 var colspan = selectObj[3] - selectObj[1]+1;
			 var d={"row":row, "col":col, "rowspan":rowspan, "colspan":colspan}
		     setting.mergeCells.push(d);
		     hot.updateSettings(setting);  
		     return this;
		}
		
		//拆分单元格
		this.aplitCells = function(){
	 		 var selectObj = hot.getSelected();
			 var row = selectObj[0];
			 var col = selectObj[1];
			 var rowspan = selectObj[2] - selectObj[0]+1;
			 var colspan = selectObj[3] - selectObj[1]+1;
			 var d={"row":row, "col":col, "rowspan":rowspan, "colspan":colspan}
		     setting.mergeCells.pop(d);
		     hot.updateSettings(setting);  
		     return this;
		}
		
		//设置背景颜色
		this.setBackground = function(backgroundColor){
			var selectObj  =hot.getSelected();
			var minbackRow  = selectObj[0]
			var minbackCol =  selectObj[1];
			var maxbackRow = selectObj[2];
			var maxbackCol = selectObj[3];
			
			for(var i=minbackRow;i<maxbackRow+1;i++){
				for(var l=minbackCol;l<maxbackCol+1;l++){
					var cell=Excel.getCell(setting.cell,i,l);
					$.extend(true,cell,{row: i, col: l, renderer: Excel.renderer, data: {backgroundColorkey:backgroundColor}})
					setting.cell.push(cell);
				}
			}
		    hot.updateSettings(setting); 	
		    return this;
		}
		
		//设置颜色
		this.setColor = function(colorcan){
			var selectObj = hot.getSelected();
			var minbackRow = selectObj[0];
			var minbackCol = selectObj[1];
			var maxbackRow = selectObj[2];
			var maxbackCol = selectObj[3];
			
			for(var i=minbackRow;i<maxbackRow+1;i++){
				for(var l=minbackCol;l<maxbackCol+1;l++){
					var cell = Excel.getCell(setting.cell, i, l);
					$.extend(true, cell, {row: i, col: l, renderer: Excel.renderer, data: {colorkey:colorcan}});
					setting.cell.push(cell);
				}
			}
		    hot.updateSettings(setting);
		    return this;
		}
		
		//加粗
		this.setBold = function(){
			var selectObj = hot.getSelected();
			var minbackRow = selectObj[0];
			var minbackCol = selectObj[1];
			var maxbackRow = selectObj[2];
			var maxbackCol = selectObj[3];
			
			for(var i=minbackRow;i<maxbackRow+1;i++){
				for(var l=minbackCol;l<maxbackCol+1;l++){
					var cell=Excel.getCell(setting.cell,i,l);
					$.extend(true,cell,{row: i, col: l, renderer: Excel.renderer, data: {fontweigth:"bold"}})
					setting.cell.push(cell);
				}
			}
		    hot.updateSettings(setting);
		    return this;
		}
		
		//取消加粗
		this.cancleBold = function(){
			var selectObj = hot.getSelected();
			var minbackRow = selectObj[0];
			var minbackCol = selectObj[1];
			var maxbackRow = selectObj[2];
			var maxbackCol = selectObj[3];
			
			for(var i=minbackRow;i<maxbackRow+1;i++){
				for(var l=minbackCol;l<maxbackCol+1;l++){
					var cell=Excel.getCell(setting.cell,i,l);
					$.extend(true,cell,{row: i, col: l, renderer: Excel.renderer, data: {fontweigth:""}})
					setting.cell.push(cell);
				}
			}
		    hot.updateSettings(setting);
		    return this;
		}
		
		//设置字体大小
		this.setFontSize = function(fontSizecan){
			var selectObj = hot.getSelected();
			var minbackRow = selectObj[0];
			var minbackCol = selectObj[1];
			var maxbackRow = selectObj[2];
			var maxbackCol = selectObj[3];
			
			for(var i=minbackRow;i<maxbackRow+1;i++){
				for(var l=minbackCol;l<maxbackCol+1;l++){
					var cell=Excel.getCell(setting.cell,i,l);
					$.extend(true,cell,{row: i, col: l, renderer: Excel.renderer, data: {fontSizekey:fontSizecan}})
					setting.cell.push(cell);
				}
			}
		    hot.updateSettings(setting);
		    return this;
		}
		
		return this;
	};
	
	//excel默认设置
	Excel.setting = {
		//默认设置,空白设置
		blank: {
		    mergeCells:[],
			//contextMenu: true,
			cell:[]
		},
		//带有行，列头
		head: {
			rowHeaders: true,
		    colHeaders: true,
		    mergeCells:[],
			contextMenu: true,
			cell:[]
		}
	}
	
	//获取单元格
	Excel.getCell = function(cells, row, col) {
		var cell = {};
		$(cells).each(function(i, e) {
			if(e.row==row && e.col==col) cell = e;
		});
		return cell;
	}
	
	//excel css设置
	Excel.renderer = function(instance,td, row, col, prop, value, cellProperties) {
		Handsontable.renderers.TextRenderer.apply(this, arguments);
		
		switch(true){
			case !!cellProperties.data.backgroundColorkey:
				td.style.backgroundColor=cellProperties.data.backgroundColorkey;

			case !!cellProperties.data.colorkey:
				td.style.color=cellProperties.data.colorkey;

			case !!cellProperties.data.fontSizekey:
				td.style.fontSize=cellProperties.data.fontSizekey;

			case !!cellProperties.data.fontweigth:
				td.style.fontWeight=cellProperties.data.fontweigth;
		}
	}
	
	window.Excel = Excel;
}()
